/*
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.teamskill.pux.library.math;

import java.util.Random;

import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.Logging;
import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.OutputPort;

/**
 * RandomNumberNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class RandomNumberNode extends AbstractNode implements Logging {

	/**
	 * CONFIG_MIN
	 */
	private static final String CONFIG_MIN = "min";

	/**
	 * CONFIG_MAX
	 */
	private static final String CONFIG_MAX = "max";

	/**
	 * OUT_DATA
	 */
	private static final String OUT_DATA = "OUT";

	/**
	 * channels
	 */
	private int channels;

	/**
	 * min
	 */
	private InputPort min;

	/**
	 * max
	 */
	private InputPort max;

	/**
	 * RandomNumberNode Constructor
	 * 
	 */
	public RandomNumberNode() {
		this("RANDOM" + nr++, false);
	}

	/**
	 * RandomNumberNode Constructor
	 * 
	 * @param name
	 * @param continuous
	 */
	public RandomNumberNode(String name, boolean continuous) {
		this(name, 1, continuous);
	}

	/**
	 * RandomNumberNode Constructor
	 * 
	 * @param outputPorts
	 */
	public RandomNumberNode(String name, int channels, boolean continuous) {
		super(name, continuous);
		this.channels = channels;
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	@Override
	public void compute() {
		Random r = new Random(System.currentTimeMillis());
		int low = (Integer) min.fetchData();
		int high = (Integer) max.fetchData();
		for (OutputPort op : getOutputPorts()) {
			int value = low + r.nextInt(high);
			LOG_NODE.finer(getNodeName() + " pushing value (" + value + ") into "
					+ op.getRemotePort().getNode().getNodeName() + "[" + op.getRemotePort().getName() + "]");
			op.pushData(new Integer(value));
		}
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	protected void createPorts() {
		min = addConfigurationPort(CONFIG_MIN, Integer.class, new Integer(0));
		max = addConfigurationPort(CONFIG_MAX, Integer.class, new Integer(Integer.MAX_VALUE));

		for (int i = 0; i < channels; i++) {
			addOutputPort(new OutputPort(OUT_DATA + (i + 1), this));
		}
	}

	/**
	 * JavaBean-style getter-method for channels
	 * 
	 * @return the channels
	 */
	public int getChannels() {
		return channels;
	}

	/**
	 * JavaBean-style setter-method for channels
	 * 
	 * @param channels
	 *            the channels to set
	 */
	public void setChannels(int channels) {
		this.channels = channels;
	}

}
